#include "protocolBase.h"

ProrocolBase::ProrocolBase(uint32_t maxBuffSize):buff_size_(maxBuffSize)
{
    rx_buffer_ = new uint8_t[buff_size_];
    recv_offset_ = 0;
    param_ = nullptr;

    // process_flag_ = false;
    // data_process_thread_ = nullptr;
}

ProrocolBase::~ProrocolBase()
{
    if(rx_buffer_)
        delete[] rx_buffer_;
}

void ProrocolBase::addCallBack(PROCESS_CALLBACK cb, void* param)
{
    package_data_callBack_ = cb;
    param_ = param;
}

int ProrocolBase::pushBuffer(const uint8_t *buf, int32_t len)
{
    if(len <= 0)
        return 0;
    for (int i = 0; i < len; i++)
	{
		if (pushByte(buf[i]))
		{   
			if (package_data_callBack_)
				package_data_callBack_(param_, rx_buffer_, recv_offset_);
			recv_offset_ = 0;
		}
	}
	return 0;
}

// int ProrocolBase::start()
// {
//     process_flag_ = true;
//     data_process_thread_.reset(new std::thread(std::bind(&ProrocolBase::data_process_loop, this)));
// }


// void ProrocolBase::data_process_loop()
// {
//     while(process_flag_){
        
//         if(protocolProcess() == 0){
//             std::this_thread::sleep_for(std::chrono::milliseconds(20));
//         }
//     }

//     printf("process loop end!\n");
// }
